package com.haidao.admin.component.service.thirdpart.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.haidao.admin.component.configuration.CryptoConfiguration;
import com.haidao.admin.component.entity.KeyCodeInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
@Slf4j
public class JiDaUtilService {

    private final CryptoConfiguration cryptoConfig;




    public Map<String, String> getReqHeaders() {
        Map<String, String> reqHeaders = new HashMap<>();
        reqHeaders.put("Authorization", getBasicAuth());
        return reqHeaders;
    }

    /**
     * 返回吉大的认证请求头
     *
     * @return
     */
    public String getBasicAuth() {
        String auth = cryptoConfig.getAppIdentity() + ":" + cryptoConfig.getPassword();
        return "Basic " + Base64.encode(auth);
    }

    /**
     * 发送post请求
     *
     * @param reqBody 请求体
     * @param url     请求地址
     * @return 响应体
     */
    public JSONObject sendPostReq(Map<String, String> headers, Map<String, Object> reqBody, String url) {
        log.info("===================================");
        log.info("hmacUrl: {}", url);
        log.info("reqHeaders: {}", headers);
        log.info("reqBody: {}", reqBody);
        log.info("===================================");
        HttpResponse execute = HttpUtil.createPost(url)
                .body(JSONUtil.toJsonStr(reqBody))
                .addHeaders(headers)
                .execute();

        log.info("respBody: {}", execute.body());
        if (execute.isOk()) {
            String body = execute.body();
            return JSONUtil.parseObj(body);
        }
        throw new RuntimeException("请求失败");
    }

    /**
     * 从服务端获取密钥  cmac:Admin@123
     * @return 密钥对象
     */
    public KeyCodeInfo getKeyCodeFromServer(String algorithm, int length, String keyUsages) {
        // 请求体
        Map<String, Object> reqBody = new HashMap<>();
        reqBody.put("algorithm", algorithm);
        reqBody.put("length", length);
        reqBody.put("keyUsages", keyUsages);

        String createKeyUrl = cryptoConfig.getBaseUrl() + cryptoConfig.getCreateKeyUri();
        Map<String, String> reqHeaders = getReqHeaders();

        JSONObject respBody = sendPostReq(reqHeaders,reqBody, createKeyUrl);

        JSONObject data = respBody.getJSONObject("data");
        String keyCode = data.getStr("keyCode");
        Date expirationTime = data.getDate("expirationTime");

        KeyCodeInfo keyCodeInfo = new KeyCodeInfo();
        keyCodeInfo.setKeyCode(keyCode);
        keyCodeInfo.setExpirationTime(expirationTime);

        return keyCodeInfo;
    }

    /**
     * 延长密钥过期时间 365天
     */
    public KeyCodeInfo extendKeyCodeExpirationTime(KeyCodeInfo keyCodeInfo) {
        String extendKeyUrl = cryptoConfig.getBaseUrl() + cryptoConfig.getExtendKeyUri();
        Map<String, Object> reqBody = new HashMap<>();
        reqBody.put("keyCode", keyCodeInfo.getKeyCode());
        reqBody.put("days", 365);

        Map<String, String> reqHeaders = getReqHeaders();

        JSONObject respBody = sendPostReq(reqHeaders, reqBody, extendKeyUrl);
        JSONObject data = respBody.getJSONObject("data");
        Date protectStopDate = data.getDate("protectStopDate");

        keyCodeInfo.setExpirationTime(protectStopDate);
        return keyCodeInfo;

    }
}
